package com.nl.lib_core.base.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.nl.lib_core.tool.log.KLog

/** 作者: wangfei 创建时间: 2021/9/6 包名: com.nl.lib_core.base.adapter 类名: BaseAdapter */
abstract class BaseAdapter<E, VH : RecyclerView.ViewHolder>(protected open var mContext: Context) :
    RecyclerView.Adapter<VH>() {
    protected var mList: ArrayList<E> = ArrayList()

    protected var mItemClickListener: OnItemClickListener<E>? = null
    protected var mOnItemLongClickListener: OnItemLongClickListener<E>? = null

    val listData: ArrayList<E>
        get() = mList

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH {
        val layoutId = onBindLayout(viewType)
        val view = LayoutInflater.from(mContext).inflate(layoutId, parent, false)
        return onCreateHolder(view, viewType)
    }

    /** 绑定布局文件 */
    protected abstract fun onBindLayout(type: Int): Int

    /** 创建一个holder */
    protected abstract fun onCreateHolder(view: View, viewType: Int): VH

    /** 绑定数据 */
    protected abstract fun onBindData(holder: VH, e: E, position: Int)

    override fun onBindViewHolder(holder: VH, position: Int) {
        val e = mList[position]
        holder.itemView.setOnClickListener {
            mItemClickListener?.onItemClick(e, position)
        }
        holder.itemView.setOnLongClickListener {
            mOnItemLongClickListener?.onItemLongClick(e, position) ?: false
        }
        onBindData(holder, e, position)
    }

    /** 获取当前item数据 */
    override fun getItemCount(): Int {
        return mList.size
    }

    /** 添加所有数据 */
    fun addAll(list: List<E>?) {
        list.takeUnless { it.isNullOrEmpty() }?.let {
            mList.addAll(it)
            notifyDataSetChanged()
        }
    }

    /** 重新加入所有数据并刷新 */
    fun refresh(list: List<E>?) {
        mList.clear()
        list.takeUnless {
            it.isNullOrEmpty()
        }?.let {
            mList.addAll(it)
        }
        notifyDataSetChanged()
    }

    /** 删除某指定位置数据 */
    fun remove(positon: Int) {
        mList.removeAt(positon)
        notifyDataSetChanged()
    }

    /** 删除数据 */
    fun remove(e: E) {
        mList.remove(e)
        notifyDataSetChanged()
    }

    /*
     *添加数据
     */
    fun add(e: E) {
        mList.add(e)
        notifyDataSetChanged()
    }

    /** 添加数据到集合里面 */
    fun addLast(e: E) {
        add(e)
    }

    /** 添加数据到手部 */
    fun addFirst(e: E) {
        mList.add(0, e)
        notifyDataSetChanged()
    }

    /** 清除数据 */
    fun clear() {
        mList.clear()
        notifyDataSetChanged()
    }

    /**
     * 设置当前item点击事件
     * @param itemClickListener 点击事件
     */
    fun setItemClickListener(itemClickListener: OnItemClickListener<E>?) {
        mItemClickListener = itemClickListener
    }

    /**
     * 设置当前item长按点击事件
     * @param onItemLongClickListener 长按点击
     */
    fun setOnItemLongClickListener(onItemLongClickListener: OnItemLongClickListener<E>) {
        mOnItemLongClickListener = onItemLongClickListener
    }

    /** item点击监听事件 */
    interface OnItemClickListener<E> {
        fun onItemClick(e: E, position: Int)
    }

    /** 长按监听事件 */
    interface OnItemLongClickListener<E> {
        fun onItemLongClick(e: E, position: Int): Boolean
    }
}
